package com.example.until;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class FileUntil {
	
	private String fileName;
	
	private int sourceLiner = 0;
	
	private String $find;
	
	public FileUntil (String fileName, String $find)
	{
		this.fileName = fileName;
		
		this.$find = $find;
	}
	
	/**
	 * 读取文件内容
	 */
	public List<String> readContent()
	{
		List<String> content = new ArrayList<String>();
		try {
			LineNumberReader reader = new LineNumberReader(new FileReader(this.fileName));
			
			String lineRead = "";
			
			int i = 0;
			
			int sourceLiner = 0;
			
			while ((lineRead = reader.readLine()) != null) {
				
				if(lineRead.indexOf(this.$find) != 1) {
					sourceLiner++;
				}
				content.add(lineRead);
            }
			this.sourceLiner = sourceLiner;
			reader.close();
			return content;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return content;
	}

	public int getSourceLiner() {
		return sourceLiner;
	}
	
	/**
	 * 指定行 写入
	 */
	public void write (String field)
	{
		List<String> content = this.readContent();
		
		int linner = this.sourceLiner + 2;
		
		
		int i = -1;
		
		for (String str : content) {
			
			i++;
			if (i < linner) {
				continue;
			}
			content.add(i, field);
			
			break;
		}
		
		System.out.println(content.toString());
		
	}
	
}
